<?php
namespace Yzadmin\Controller;
use Think\Controller;
/**
 * 后台公共控制器
 */
class PublicController extends Controller {

  public function index()
  {
      check_login();
      $this->display();
  }


  /*管理员登录*/
  public function login(){
      if (IS_POST){
          $username=I('post.username');
          $password=I('post.password');
          $verify=I('post.verify');
          if (!check_verify($verify)){
              $this->error('验证码错误！',U('login'));
          }
          $user=D('Admin')->where(array('username'=>I('username')))->field(true)->find();
          if (empty($user)){
              $this->error('管理员不存在！',U('login'));
          }

          if ($user['userpwd'] !== md5($password)) {
              $this->error('密码错误！',U('login'));
          }
          $auth = array(
                  'uid'             => $user['id'],
                  'username'        => $user['username']
          );
          $last_login_time=date('Y-m-d H:i:s',$user['last_login_time']);
          //创建session
          session('user_auth', $auth);
          session('last_login_time', $last_login_time);
          session('last_login_ip', $user['last_login_ip']);
          session('system_level', $user['level']);
          //更新登录时间和登录ip
          $data['last_login_time'] = time();
          $data['last_login_ip'] =  $_SERVER["REMOTE_ADDR"];
          D('Admin')->where('id='.$user['id'])->save($data);
          $this->redirect('Index/index');
      }

      $this->display();

  }

  /*退出*/
  public function logout()
  {
      session('user_auth',null);
      $this->success('退出成功');
      $this->redirect('Public/login');

  }

  /*左侧菜单栏*/
  public function menu_nav($mid = 0)
  {
      if ($mid==0){$mid=1;}
      $tree_list = get_menu_tree($mid);
      $this->assign('tree_list',$tree_list);
      $this->display();
  }

    /* 验证码 */
  public function verify(){
      ob_clean();
      $verify = new \Think\Verify();
      $verify->length = 4;
      $verify->fontSize = 25;
      $verify->entry();
  }

  /* 更新全站缓存 */
  public function deleteCache() {
    $srcurl = I('srcurl');
    delete_dir(RUNTIME_PATH);
    $this->success('更新成功',$srcurl);
  }

  public function getAjaxPassWord(){
    $admin_id = I('post.uadminid');if(empty($admin_id)){echo 0;exit;}
    $upass = I('post.upass');$upass = md5($upass);
    $admin_user = D('Admin')->find($admin_id);if(empty($admin_user)){echo 0;exit;}
    if($upass != $admin_user['userpwd']){echo 0;exit;}else{echo 1;exit;}
  }

}
